package com.example.liwushuo.fragment;

import com.alibaba.fastjson.JSONObject;
import com.example.liwushuo.BaseApp;
import com.example.liwushuo.R;
import com.example.liwushuo.bar.HomeBar;
import com.example.liwushuo.bean.BaseTab;
import com.example.liwushuo.bean.TabTitle;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class HomeFragment extends Fragment {

	private String url;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		url = getArguments().getString("url");
		//Log.i("HomeFragment", "--------"+url);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.home_fragment, null);
		getFragmentManager().beginTransaction()
				.replace(R.id.home_bar_id, new HomeBar()).commit();
		initTabTitle();
		return view;
	}

	private void initTabTitle() {

		BaseApp.getHttpUtils().send(HttpMethod.GET, url,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {

						Toast.makeText(getActivity(), "请检查网络，网络连接失败", 1).show();
					}

					@Override
					public void onSuccess(ResponseInfo<String> response) {
						String json = response.result;
						
						//Log.i("homeFragment", "------------"+json);
						BaseTab baseTab = JSONObject.parseObject(json,
								BaseTab.class);
						if (baseTab != null) {
							TabTitle data = baseTab.getData();
						//	Log.i("data", "----" + data);
							if (data != null) {
								HomeInfo home_info = new HomeInfo();
								Bundle args = new Bundle();
								args.putSerializable("data", data);
								home_info.setArguments(args);
								getFragmentManager().beginTransaction()
										.replace(R.id.home_info, home_info)
										.commit();
							}
						}
					}
				});
	}
}
